package com.OrderingSystem.controller.seller;

import com.OrderingSystem.pojo.ProductInfo;
import com.OrderingSystem.service.IProductInfoService;
import com.OrderingSystem.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/seller/productInfo")
@Api(tags = "餐品信息管理接口")
@ApiResponses({
        @ApiResponse(code = 200, message = "请求成功",response = Result.class),
        @ApiResponse(code = 403, message = "没有权限"),
        @ApiResponse(code = 404, message = "资源不存在"),
        @ApiResponse(code = 500, message = "服务器错误")
})
public class ProductInfoController {

    @Autowired
    IProductInfoService productInfoService;

    @Autowired
    RedisTemplate redisTemplate;

    // 查询全部餐品
    @GetMapping("/list")
    @ApiOperation(value = "查询全部餐品(不带分页)", notes = "查询全部餐品(不带分页)")
    public Result selectAllProductInfo() {
        return Result.success(productInfoService.list());
    }

    // 查询全部餐品(带分页)
    @GetMapping("/page")
    @ApiOperation(value = "查询全部餐品(带分页)", notes = "查询全部餐品(带分页)")
    public Result selectAllProductInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                       ProductInfo productInfo) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductInfo> productInfos = productInfoService.selectAllProductInfo(pageNum, pageSize, productInfo);
        PageInfo<ProductInfo> pageInfo = new PageInfo<>(productInfos);
        return Result.success(pageInfo);
    }

    // 查询全部餐品(带分类名称)
    @GetMapping("/pageWithCategoryName")
    @ApiOperation(value = "查询全部餐品(带分类名称)", notes = "查询全部餐品(带分类名称)")
    public Result selectAllProductInfoWithCategoryName(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                       ProductInfo productInfo) {
        List<Map<String, Object>> productInfos = productInfoService.selectAllProductInfoWithCategoryName((pageNum - 1) * pageSize, pageSize, productInfo);
        int total = productInfoService.countAllProductInfoWithCategoryName(productInfo);

        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(productInfos);
        pageInfo.setTotal(total);
        return Result.success(pageInfo);
    }

    // 根据ID查询餐品
    @GetMapping("/{productId}")
    @ApiOperation("根据ID查询餐品")
    public Result selectProductInfoById(@PathVariable(value = "productId") Integer productId) {
        return Result.success(productInfoService.getById(productId));
    }

    // 添加餐品
    @PostMapping("/add")
    @ApiOperation("添加餐品")
    public Result add(@RequestBody ProductInfo productInfo) {
        // 清空redis缓存
        redisTemplate.delete("productVOList");
        return productInfoService.save(productInfo) ? Result.success(true) : Result.error("添加失败");
    }

    // 修改餐品
    @PutMapping("/update")
    @ApiOperation("修改餐品")
    public Result update(@RequestBody ProductInfo productInfo) {
        // 清空redis缓存
        redisTemplate.delete("productVOList");
        return productInfoService.updateById(productInfo) ? Result.success(true) : Result.error("修改失败");
    }

    // 根据ID删除餐品
    @DeleteMapping("/delete/{productId}")
    @ApiOperation("根据ID删除餐品")
    public Result delete(@PathVariable(value = "productId") Integer productId) {
        // 清空redis缓存
        redisTemplate.delete("productVOList");
        return productInfoService.removeById(productId) ? Result.success(true) : Result.error("删除失败");
    }

}
